/**
 * Created by John on 2016/5/23.
 */

var demo = new sample();

// demo.module();

// demo.express();

// demo.hello();

// demo.routes();

// demo.rest();

// demo.next();

// demo.users();

// demo.layout();

// demo.otherLayout();

demo.partialView();

function sample() {
    this.name = "第5章";

    this.module = function () {
        var http = require("http");
        var querystring = require("querystring");
        var server = http.createServer(function (req, res) {
            var post;
            req.on("data", function (chunk) {
                post += chunk;
            });

            req.on("end", function () {
                post = querystring.parse(post);
                res.write(post.title);
                res.write(post.text);
                res.end();
            });
        }).listen(3000);
    };

    this.express = function () {
        var express = require("express");
        var app = express();

        app.all("/", function (req, res) {
            res.send(req.body.title + req.body.text);
        });

        app.listen(3000);
    };

    this.hello = function () {
        var express = require('express');
        var app = express();
        var data = express.Router();

        data.get('/', function (req, res, next) {
            var now = new Date();
            var message = 'hello world, ' + now;
            res.send(message);
        });
        data.get('/time', function (req, res, next) {
            var now = new Date();
            var hours = now.getHours();
            var minutes = now.getMinutes();
            var message = 'The time is ' + hours + ':' + minutes;
            res.send(message);
        });
        app.use('/data', data);

        app.listen(3000);
    };

    this.routes = function () {
        var express = new require('express');
        var app = new express();

        app.get('/user/:username', function (req, res) {
            res.send('user ' + req.params.username);
        });

        app.listen(3000);
    };

    this.rest = function () {
        var express = require('express');
        var app = express();

        var username = '/user/:username';
        app.all(username, function (req, res) {
            res.send('all methods captured');
        });
        app.get(username, function (req, res) {
            res.send('user: ' + req.params.username);
        });

        app.listen(3000);
    };

    this.next = function () {
        var express = require('express');
        var app = express();

        var username = '/user/:username';

        app.all(username, function (req, res, next) {
            console.log('all methods captured');
            next();
        });
        app.get(username, function (req, res) {
            res.send('user: ' + req.params.username);
        });

        app.listen(3000);
    };

    this.users = function () {
        var express = require('express');
        var app = express();

        var users = {
            'byvoid': {
                name: 'Carbo',
                website: 'http://byoivd.com'
            }
        };

        var usernameUri = '/user/:username';
        app.all(usernameUri, function (req, res, next) {
            if (users[req.params.username]) {
                next();
            } else {
                next(new Error(req.params.username + ' does not exist.'));
            }

        });
        app.get(usernameUri, function (req, res) {
            var name = JSON.stringify(users[req.params.username]);
            res.send(name);
        });
        app.put(usernameUri, function (req, res) {
            res.send('Done');
        })

        app.listen(3000);
    };

    this.layout = function () {
        var express = require('express');
        var app = express();
        var home = require('./routes/index');

        app.set('views', __dirname + '/views');
        app.set('view engine', 'ejs');
        app.set('view options', {layout: false});

        app.use(express.static('public'));
        app.use('/', home);

        app.listen(3000);
    };

    this.otherLayout = function () {
        var express = require('express');
        var app = express();
        var users = require('./routes/users');

        app.set('views', __dirname + '/views');
        app.set('view engine', 'ejs');

        app.use(express.static('public'));
        app.use('/', users);

        app.listen(3000);
    };

    this.partialView = function () {
        var express = require('express');
        var app = express();
        var list = require('./routes/users');

        app.set('views', __dirname + '/views');
        app.set('view engine', 'ejs');

        app.use(express.static('./public'));
        app.use('/', list);

        app.get('/list', function (req, res) {
            res.render('list', {
                title: 'List',
                items: [1991, 'byvoid', 'express', 'Node.js']
            });
        });

        app.listen(3000);
    };

    this.viewHelpers = function () {
        var express = require('express');
        var app = express();
        var util = require('util');

        app.set('views', __dirname + '/views');
        app.set('view engine', 'ejs');

        app.use(express.static('./public'));

        app.helpers({
            inspect: function (obj) {
                return util.inspect(obj, true);
            }
        });

        app.dynamaicHelpers({
            headers: function (req, res) {
                res.render('helper', {
                    title: 'helpers'
                });
            }
        });

        app.listen(3000);
    }
}